/**
 */
package com.dingpeikeji.quickdeer.core.modules.schedef.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.parsing.GenericTokenParser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.bus.entity.TCompBus;
import com.dingpeikeji.quickdeer.core.modules.schedef.entity.TSchedef;
import com.dingpeikeji.quickdeer.core.modules.schedef.entity.TSchedefAudit;
import com.dingpeikeji.quickdeer.core.modules.schedef.service.TSchedefAuditService;
import com.dingpeikeji.quickdeer.core.modules.schedef.service.TSchedefService;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.User;
import com.dingpeikeji.quickdeer.core.modules.sys.service.SystemService;
import com.dingpeikeji.quickdeer.core.modules.sys.utils.UserUtils;

/**
 * 班次审核Controller
 * @author Zhaoy
 * @version 2016-06-22
 */
@Controller
@RequestMapping(value = "${adminPath}/schedef/tSchedefAudit")
public class TSchedefAuditController extends BaseController {

	@Autowired
	private TSchedefAuditService tSchedefAuditService;
	@Autowired
	private TSchedefService tSchedefService;
	@Autowired
	private SystemService systemService;
	@ModelAttribute
	public TSchedefAudit get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return tSchedefAuditService.get(id);
		}else{
			return new TSchedefAudit();
		}
	}
	
	@RequiresPermissions("schedef:tSchedefAudit:view")
	@RequestMapping(value = {"list", ""})
	public String list(@ModelAttribute("tSchedef")TSchedef tSchedef, HttpServletRequest request, HttpServletResponse response, Model model) {
		tSchedef.setAuditstatus("3");
		Page<TSchedef> page = tSchedefService.findPage(new Page<TSchedef>(request, response), tSchedef);
		model.addAttribute("page", page);
		return "modules/schedef/tSchedefAuditList";
	}

	@RequiresPermissions("schedef:tSchedefAudit:view")
	@RequestMapping(value = "form")
	public String form(TSchedefAudit tSchedefAudit, Model model,HttpServletRequest request) {
		/*//当班次未审核时
		if(tSchedefAudit.getOperaterid()==null){
			tSchedefAudit.setOperaterid(UserUtils.getUser().getName());
		}
		//当班次已审核时
		else{
			//User user = new User();
			//user.setId(tSchedefAudit.getOperaterid());
			tSchedefAudit.setOperaterid(systemService.getUser(tSchedefAudit.getOperaterid()).getName());
		}*/
		TSchedefAudit tSchedefAudit1 = new TSchedefAudit();
		tSchedefAudit1.setOperaterid(UserUtils.getUser().getName());
		model.addAttribute("scheid", request.getParameter("id"));
		model.addAttribute("tSchedefAudit", tSchedefAudit1);
		return "modules/schedef/tSchedefAuditForm";
	}

	@RequiresPermissions("schedef:tSchedefAudit:edit")
	@RequestMapping(value = "save")
	public String save(TSchedefAudit tSchedefAudit, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request) {
		if (!beanValidator(model, tSchedefAudit)){
			return form(tSchedefAudit, model,request);
		}
		tSchedefAudit.setOperaterid(UserUtils.getUser().getId());
		String scheid = request.getParameter("scheid");
		tSchedefAudit.setScheid(scheid);
		TSchedef tSchedef = tSchedefService.get(scheid);
		//审核是否通过 1 通过 2 不通过
		if (tSchedefAudit.getAuditopn().equals("1")) {
			tSchedef.setAuditstatus("1");
		}else if(tSchedefAudit.getAuditopn().equals("2")){
			tSchedef.setAuditstatus("2");
		}
		tSchedefAuditService.save(tSchedefAudit);
		tSchedefService.update(tSchedef);
		addMessage(redirectAttributes, "保存班次审核成功");
		return "redirect:"+Global.getAdminPath()+"/schedef/tSchedefAudit/?repage";
	}
	
	@RequiresPermissions("schedef:tSchedefAudit:edit")
	@RequestMapping(value = "delete")
	public String delete(TSchedefAudit tSchedefAudit, RedirectAttributes redirectAttributes) {
		tSchedefAuditService.delete(tSchedefAudit);
		addMessage(redirectAttributes, "删除班次审核成功");
		return "redirect:"+Global.getAdminPath()+"/schedef/tSchedefAudit/?repage";
	}

}
